package com.xw.unification.utils.http.client;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

public abstract class HttpClientCommonBuilder {

    protected HttpClientBuilder clientBuilder;

    protected HttpClientCommonBuilder(HttpClientBuilder clientBuilder) {
        this.clientBuilder = clientBuilder;
    }

    public HttpRequestBuilder get(String uri) {
        return get(uri, null);
    }

    public HttpRequestBuilder get(String uri, RequestConfig requestConfig) {
        HttpGet httpRequest = new HttpGet(uri);
        if (requestConfig != null) {
            httpRequest.setConfig(requestConfig);
        }
        return new HttpRequestBuilder(httpRequest, build());
    }

    public HttpRequestBuilder delete(String uri) {
        return delete(uri, null);
    }

    public HttpRequestBuilder delete(String uri, RequestConfig requestConfig) {
        HttpDelete httpRequest = new HttpDelete(uri);
        if (requestConfig != null) {
            httpRequest.setConfig(requestConfig);
        }
        return new HttpRequestBuilder(httpRequest, build());
    }

    public HttpRequestBodyBuilder post(String uri) {
        return post(uri, null);
    }

    public HttpRequestBodyBuilder post(String uri, RequestConfig requestConfig) {
        HttpPost httpRequest = new HttpPost(uri);
        if (requestConfig != null) {
            httpRequest.setConfig(requestConfig);
        }
        return new HttpRequestBodyBuilder(httpRequest, build());
    }

    public HttpRequestBodyBuilder put(String uri) {
        return put(uri, null);
    }

    public HttpRequestBodyBuilder put(String uri, RequestConfig requestConfig) {
        HttpPut httpRequest = new HttpPut(uri);
        if (requestConfig != null) {
            httpRequest.setConfig(requestConfig);
        }
        return new HttpRequestBodyBuilder(httpRequest, build());
    }

    public CloseableHttpClient build() {
        return clientBuilder.build();
    }

}

